var http = require('http');
var cheerio = require('cheerio');
var iconv = require('iconv-lite');

var websiteConfig = {
    jiayuan : {
        url: 'http://www.077455.com/forum-50-1.html',
        encode: 'gbk'
    }
}

var infoDataList = [];
var idCacheList = [];

// 爬取岑溪家园网招聘信息
http.get(websiteConfig.jiayuan.url, function (sres) {
    var chunks = [];
    sres.on('data', function (chunk) {
        chunks.push(chunk);
    });
    sres.on('end', function () {
        parseList(chunks,websiteConfig.jiayuan.encode);
        goDetailLink();
        console.log(infoDataList);
    });
});


/**
 * 解析招聘列表 for 岑溪家园网
 * @param {String} chunks 
 */
function parseList(chunks, encode) {
    //由于咱们发现此网页的编码格式为gb2312，所以需要对其进行转码，否则乱码
    //依据：“<meta http-equiv="Content-Type" content="text/html; charset=gb2312">”
    var html = iconv.decode(Buffer.concat(chunks), encode);
    var $ = cheerio.load(html, { decodeEntities: false });

    var $infoDomList = [];
    // 只抓取普通的招聘信息，忽略网站顶置的招聘信息
    $('table#threadlisttableid').find('tbody').each(function (idx, element) {
        var elemId = $(element).attr('id');
        // console.log(elemId);
        if (elemId.indexOf('normalthread_') != -1) {
            $infoDomList.push($(element));
        }
    });

    // 解析招聘列表
    $infoDomList.forEach(function (infoTbody, idx) {
        var $infoTbody = $(infoTbody);
        var $link = $infoTbody.find('th.new a.s.xst');
        var $opreation = $infoTbody.find('th.new a.showcontent.y');

        var infoId = $opreation.attr('id').replace(/content_/, '');
        if(!idCacheList.includes(infoId)){
            idCacheList.push(infoId);
            infoDataList.push({
                id: infoId,
                title: $link.text(),
                link: $link.attr('href'),
            });
        }
    });
}


/**
 * 根据url获取招聘信息
 */
function getInfoDesc(item){
    return new Promise((resolve, reject) => {
        http.get('http://www.077455.com/'+ item.link, function(sres){
            var chunks = [];
            sres.on('data', function(chunk) {
                chunks.push(chunk);
            });
            sres.on('end', function() {
                var html = iconv.decode(Buffer.concat(chunks), 'gbk');
                var $ = cheerio.load(html, {decodeEntities: false});

                var $div = $('div#ct div#postlist > div[id^="post_"] table.plhin div.pct div.t_fsz table');
                var text = $div.text();
                resolve(text);
            });
        });
    });
}

/**
 * 访问招聘详情页
 */
function goDetailLink(){
    infoDataList.forEach(async function(infoItem,idx){
        var desc = await getInfoDesc(infoItem);
        infoItem.desc = desc;
    });
}